/**
 * 一次加载一组，卡片切换效果
 */
const app = getApp();
const innerAudioContext = wx.createInnerAudioContext();
const pageSize = 5;
Page({
  data: {
    startX: 0,
    endX: 0,
    nowPage: 0,//滑动页的第几个
    wordPage: 1,//第几大页，一页pageSize个。
    dbName:"words",
    wordCount:null,
    wordLevel: null,
    wordList:null
  },

  onLoad: function (options) {
     
    let that = this;
    let type = options.type;
    //查询的数据库名称

    if (type == "phrase")
    {
      this.data.dbName = "word_phrase";
    }
    if (type == "root") {
      this.data.dbName = "word_root";
    }

    let count = parseInt(options.count);
    let level = parseInt(options.level);

    this.setData({
      wordCount: count,
      wordLevel: level
    });

    let filter = {};
    if ("word_root"==this.data.dbName)
    {
      //词根不分级别分类型
      filter.type=level;
    }else
    {
      filter.level = level;
    }

    this.data.filter = filter;

    //查询缓存，看原来已经看到哪里了
    let wordPage = wx.getStorageSync(this.data.dbName + "_" + this.data.wordLevel);
    //如果缓存的不为空，
    if (null != wordPage && parseInt(wordPage)>0) {
      this.setData({
        wordPage: wordPage
      });
    }
    this.loadPage(0);
  },
  //点击播放录音
  gotoPlay: function (e) {
    var filePath = e.currentTarget.dataset.key;

    if (null == filePath || "" == filePath) {
      filePath = this.data.wordResult.audio;
    }
    innerAudioContext.src = filePath;
    console.info(filePath);
    innerAudioContext.play();
  },
  //移动手指
  moveStart: function (e) {
    var startX = e.changedTouches[0].pageX;
    this.setData({
      startX: startX
    });
  },
  
  //滑动
  moveItem: function (e) {
    var that = this;
    var endX = e.changedTouches[0].pageX;
    this.setData({
      endX: endX
    });

    //计算手指触摸偏移剧距离
    var moveX = this.data.startX - this.data.endX;

    //向左移动
    if (moveX > 20) {

      if (that.data.nowPage >= (that.data.wordList.length - 1)) {
        wx.showToast({
          title: '最后一题了',
          icon: 'none'
        })

        //到最后一题,加载下一个pageSize个题目
        that.loadPage(1);

        return false;
      }
      that.setData({
        nowPage: that.data.nowPage + 1
      });
      this.checkPage(this.data.nowPage);
    }
    if (moveX < -20) {
      if (that.data.nowPage <= 0) {
        that.loadPage(-1);
        return false;
      }
      that.setData({
        nowPage: that.data.nowPage - 1
      });
      this.checkPage(this.data.nowPage);
    }


  },

  //从数据库加载下一组单词
  loadPage: function(ran) {

    if (-1 == ran && 1 == this.data.wordPage)
    {
      wx.showToast({
        title: '前面没有单词了',
        icon:'none'
      })
      return;
    }

    if (1 == ran && this.data.wordCount / pageSize == this.data.wordPage)
    {
      wx.showToast({
        title: '后面没有单词了',
        icon:'none'
      })
      return;
    }
    
    wx.showLoading({
      icon:"none",
      title: "拼命加载中"
    });
    let that =this;
    let wordPage = this.data.wordPage + ran;

    //缓存已经看到的位置，只记第几大页
    wx.setStorageSync(this.data.dbName + "_" + this.data.wordLevel, wordPage);
    
    //调用分页云函数
    wx.cloud.callFunction({
      name: 'pagination',
      data: {
        dbName: this.data.dbName,
        pageIndex: wordPage,
        pageSize: pageSize,
        filter: this.data.filter
      },
      success(res) {
        wx.hideLoading();
        var wordList = res.result.data;
        let nowPage =0;
        //发果是向前滑一页，则默认跳到第pageSize个。
        if (-1 == ran)
        {
          nowPage = pageSize;
        }

        that.setData({
          wordPage: wordPage,
          wordList: wordList,
          nowPage: nowPage
        });
        that.checkPage(nowPage);
      },
      fail() {
      }
    });

  },

  //选择页数
  checkPage: function (index) {
    //信列表数据
    var data = this.data.wordList;
    var that = this;
    var m = 1;
    for (var i = 0; i < data.length; i++) {
      //先将所有的页面隐藏
      var disp = 'wordList[' + i + '].display';
      var sca = 'wordList[' + i + '].scale';
      var slateX = 'wordList[' + i + '].slateX';
      var zIndex = 'wordList[' + i + '].zIndex';
      var style = 'wordList[' + i + '].style';
      that.setData({
        [disp]: 0,
        [style]: "display:block",
      });
      //向左移动上一个页面
      if (i == (index - 1)) {
        that.setData({
          [slateX]: '-120%',
          [disp]: 1,
          [zIndex]: 2,

        });
      }
      //向右移动的最右边要display:none的页面
      if (i == (index + 3)) {
        that.setData({
          [style]: 'display:none',
          [slateX]: '0',
          [zIndex]: -10,
        });
      }
      if (i == index || (i > index && (i < index + 3))) {
        //显示最近的三题
        that.setData({
          [disp]: 1
        });
        //第一题
        if (m == 1) {
          this.setData({
            [sca]: 1,
            [slateX]: 0,
            [zIndex]: 1,
          });
        }
        //第二题
        else if (m == 2) {
          this.setData({
            [sca]: 0.8,
            [slateX]: '20px',
            [zIndex]: -1,
          });
        }
        //第三封信
        else if (m == 3) {
          this.setData({
            [sca]: 0.6,
            [slateX]: '40px',
            [zIndex]: -2,
          });
        }
        m++;
      }

    }
  }
})